package com.cxh.rmsq.ui.adapter.self

import android.content.Intent
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cxh.rmsq.R
import com.cxh.rmsq.common.IntentExtra
import com.cxh.rmsq.db.model.CommentChildren
import com.cxh.rmsq.ui.activity.self.LookUserDetailActivity2
import com.cxh.rmsq.ui.activity.self.PreViewImgOrVideoActivity
import com.cxh.rmsq.ui.widget.SelectableRoundedImageView
import com.cxh.rmsq.utils.loadImage
import com.hjq.shape.layout.ShapeRelativeLayout
import com.hjq.shape.view.ShapeImageView


/**
 *
 */
class CommentListAdapter(type: Int, private val listener: OnCustomClickListener) :
    BaseQuickAdapter<CommentChildren, BaseViewHolder>(R.layout.item_comment_parent_list),
    LoadMoreModule {
    private val mListener: OnCustomClickListener

    init {
        addChildClickViewIds(R.id.iv_like)
        this.mListener = listener
    }

    var type = type
    private var mData = mutableListOf<String>() //图片

    override fun convert(holder: BaseViewHolder, item: CommentChildren) {
        with(holder) {

            setText(R.id.tv_name, item.nickname)
            setText(
                R.id.tv_time,
                TimeUtils.getFriendlyTimeSpanByNow(item.createTime) + " · " + item.location + "  回复"
            )
            if (!TextUtils.isEmpty(item.content)) {
                setText(R.id.tv_content, item.content)
                getView<TextView>(R.id.tv_content).isVisible = true
            } else {
                getView<TextView>(R.id.tv_content).isVisible = false
            }
            setText(R.id.tv_like, item.likedCount.toString())

            loadImage(item.avatar, holder.getView<SelectableRoundedImageView>(R.id.siv_header))
//            Glide.with(context)
//                .load(item.avatar)
//                .placeholder(R.mipmap.image_zanwei)
//                .into(holder.getView<SelectableRoundedImageView>(R.id.siv_header))

            if (!TextUtils.isEmpty(item.imgUrl)) {
                holder.getView<ShapeImageView>(R.id.siv_img).isVisible = true
                loadImage(item.imgUrl, holder.getView<ShapeImageView>(R.id.siv_img))
            }
            holder.getView<ShapeImageView>(R.id.siv_img).setOnClickListener {
                context.startActivity(
                    Intent(context, PreViewImgOrVideoActivity::class.java)
                        .putExtra("type", 1)
                        .putExtra("url", item.imgUrl)
                )
            }

            if (!TextUtils.isEmpty(item.isLiked.toString()) && item.isLiked == 1) {
                holder.getView<ImageView>(R.id.iv_like)
                    .setImageDrawable(context.resources.getDrawable(R.mipmap.icon_cicrle_liked))
            } else {
                holder.getView<ImageView>(R.id.iv_like)
                    .setImageDrawable(context.resources.getDrawable(R.mipmap.icon_cicrle_no_like))
            }
            if (item.vipLevel == 0) {//不是会员
                holder.getView<ImageView>(R.id.sll_vip).isVisible = false
            } else {
                holder.getView<ImageView>(R.id.sll_vip).isVisible = true
            }

            if (item.children != null && item.children!!.size > 0) {
                holder.getView<RecyclerView>(R.id.rv_child).isVisible = true
                var mAdapter = CommentChildListAdapter()
                holder.getView<RecyclerView>(R.id.rv_child).layoutManager =
                    LinearLayoutManager(context)
                holder.getView<RecyclerView>(R.id.rv_child).adapter = mAdapter
                mAdapter.setNewInstance(item.children)
                mAdapter.setOnItemClickListener { adapter, view, position ->
                    if (listener != null) {
                        listener.onItemClick(holder.layoutPosition, item.children!!.get(position));
                    }
                }
                mAdapter.setOnItemChildClickListener { adapter, view, position ->
                    when (view.id) {
                        R.id.iv_like -> {
                            if (listener != null) {
                                listener.onItemChildClick(holder.layoutPosition, item.children!!.get(position));
                            }
                        }

                    }
                }
            } else {
                holder.getView<RecyclerView>(R.id.rv_child).isVisible = false
            }

            holder.getView<ShapeRelativeLayout>(R.id.sll).setOnClickListener {
                val intent = Intent(context, LookUserDetailActivity2::class.java)
                intent.putExtra(IntentExtra.STR_TARGET_ID, item.userId.toString())
                intent.putExtra("isContact", "1")
                context.startActivity(intent)
            }

        }
    }

    inner class CommentChildListAdapter() :
        BaseQuickAdapter<CommentChildren, BaseViewHolder>(R.layout.item_comment_parent_list),
        LoadMoreModule {

        init {
            addChildClickViewIds(R.id.iv_like)
        }


        private var mData = mutableListOf<String>() //图片

        override fun convert(holder: BaseViewHolder, item: CommentChildren) {
            with(holder) {

                setText(R.id.tv_name, item.nickname)
                setText(
                    R.id.tv_time,
                    TimeUtils.getFriendlyTimeSpanByNow(item.createTime) + " · " + item.location + "  回复"
                )
                if (!TextUtils.isEmpty(item.content)) {
                    if (!TextUtils.isEmpty(item.commentedNickname)) {
                        val spannable = SpannableString("@" + item.commentedNickname + ": " + item.content)
                        val blueSpan = ForegroundColorSpan(Color.RED)
                        spannable.setSpan(blueSpan, 0, spannable.split(" ")[0].length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) // "Hello" 变蓝
                        setText(R.id.tv_content, spannable)
                    } else {
                        setText(R.id.tv_content, item.content)
                    }
                    getView<TextView>(R.id.tv_content).isVisible = true
                } else {
                    getView<TextView>(R.id.tv_content).isVisible = false
                }
                setText(R.id.tv_like, item.likedCount.toString())

            loadImage(item.avatar, holder.getView<SelectableRoundedImageView>(R.id.siv_header))
//                Glide.with(context)
//                    .load(item.avatar)
//                    .placeholder(R.mipmap.image_zanwei)
//                    .into(holder.getView<SelectableRoundedImageView>(R.id.siv_header))

                if (!TextUtils.isEmpty(item.imgUrl)) {
                    holder.getView<ShapeImageView>(R.id.siv_img).isVisible = true
                    loadImage(item.imgUrl, holder.getView<ShapeImageView>(R.id.siv_img))
                }
                holder.getView<ShapeImageView>(R.id.siv_img).setOnClickListener {
                    context.startActivity(
                        Intent(context, PreViewImgOrVideoActivity::class.java)
                            .putExtra("type", 1)
                            .putExtra("url", item.imgUrl)
                    )
                }

                if (!TextUtils.isEmpty(item.isLiked.toString()) && item.isLiked == 1) {
                    holder.getView<ImageView>(R.id.iv_like)
                        .setImageDrawable(context.resources.getDrawable(R.mipmap.icon_cicrle_liked))
                } else {
                    holder.getView<ImageView>(R.id.iv_like)
                        .setImageDrawable(context.resources.getDrawable(R.mipmap.icon_cicrle_no_like))
                }

                holder.getView<ShapeRelativeLayout>(R.id.sll).setOnClickListener {
                    val intent = Intent(context, LookUserDetailActivity2::class.java)
                    intent.putExtra(IntentExtra.STR_TARGET_ID, item.userId.toString())
                    intent.putExtra("isContact", "1")
                    context.startActivity(intent)
                }

            }
        }

    }


    interface OnCustomClickListener {
        fun onItemClick(position: Int, data: CommentChildren?) // 根据需求定义参数
        fun onItemChildClick(position: Int, data: CommentChildren?) // 根据需求定义参数
    }
}